<?php

namespace App\Controllers;

class Travel extends BaseController
{
    public function index()
    {
        $places = new \App\Models\Places();
        $records = $places->findAll();
        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        foreach ($records as $record) {
            $nameLink = anchor("travel/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->description);
        }
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border:1px solid #dddddd">',
            'row_alt_start' => '<tr style="background-color: #dddddd">',
        ];
        $table->setTemplate($template);
        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright Firefly',
        ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)->render('templates\top') . $table->generate() . $parser->setData($fields)->render('templates\bottom');

        //return $table->generate();
        //return $parser->setData(['records'=>$records])->render('placeslist');
    }
    public function showme($id)
    {
        $places = new \App\Models\Places();
        $record = $places->find($id);
        
        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
        array_map('ucfirst', $headings);
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border:1px solid #dddddd">',
            'row_alt_start' => '<tr style="background-color: #dddddd">',
            'heading_cell_start' => '<th style="background-color: #dddddd">',
            'heading_cell_end' => '<th style="background-color: #dddddd">',
        ];
        
        $table->setTemplate($template);

        $fields = [
            'title' => 'One Travel Destination',
            'heading' => 'Travel Destination',
            'footer' => 'Copyright Firefly',
        ];
        
        $parser = \Config\Services::parser();

        $table->addRow($headings[0], $record[$headings[0]]);
        $table->addRow($headings[1], $record[$headings[1]]);
        $table->addRow($headings[2], $record[$headings[2]]);
        $table->addRow($headings[3], $record[$headings[3]]);
        $table->addRow($headings[4],'<img src="/image/'.$record[$headings[4]].'"/>');
        
        return $parser->setData($fields)->render('templates\top') . $table->generate() . $parser->setData($fields)->render('templates\bottom');
    }

}